<template>
  <div >
    <!-- 添加商品按钮 -->
    <div>
      <div class="mt-4">
        <el-input
            v-model="searchQuery"
            style="max-width: 600px"
            placeholder="请按规则输入查询内容"
            class="input-with-select"
        >
          <template #prepend>
            <el-select v-model="select" placeholder="请选择查询规则" style="width: 115px">
              <el-option label="按商品名称或者关键字查询" value="1"/>
              <el-option label="按总价价格区间查询，~，例如，小于500为~500，大于500为500~,大于200小于500为200~500" value="2"/>
            </el-select>
          </template>
          <template #append>
            <el-button :icon="Search" @click="search"/>
          </template>
        </el-input>
        <el-button plain size="small" @click="clearSearch" style="margin-left: 10px">重置</el-button>
      </div>
    </div>
    <!-- 商品列表表格 -->
    <el-table :data="orders" style="width: 100%">
      <el-table-column  label="序号" width="80">
         <template #default="scope">
             <el-button type="text"  @click="getDetail(scope.row)">
               {{scope.row.orderId}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" width="200"></el-table-column>
      <el-table-column label="商品图片" width="200">
        <template #default="scope">
        <img :src="scope.row.productImg" alt="商品图片" style="width: 100px; height: 100px">
        </template>
      </el-table-column>
      <el-table-column prop="productDescription" label="商品描述" width="200"></el-table-column>
      <el-table-column label="单价" width="80">
           <template #default="scope">
          {{ scope.row.singleMoney / 100 }} 元
        </template>
      </el-table-column>
      <el-table-column prop="productNum" label="购买数量" width="80"></el-table-column>
      <el-table-column prop="orderTime" label="订购时间" width="150"></el-table-column>
      <el-table-column prop="postcode" label="邮编" width="80"></el-table-column>
      <el-table-column label="总价" width="100">
        <template #default="scope">
          {{ scope.row.totalMoney / 100 }} 元
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="totalOrders"
        @current-change="handlePageChange"
        style="margin-top: 10px"
    />
  </div>
</template>
<script setup>
import {onMounted, ref} from 'vue';
import {ElButton, ElMessage} from 'element-plus';
import {Search} from '@element-plus/icons-vue'
import {useRouter} from 'vue-router';
import {
  getOrderByTime,
  getOrderByProductName,
  getOrderByPriceScope
} from '@/api/user.js'
const searchQuery = ref('')
const router = useRouter();
const orders = ref([]);
const currentPage = ref(1);
const select = ref('')
const pageSize = 3;
const totalOrders = ref(0);
import { useTokenStore} from "@/store/tokenStore.js";

const search = () => {
  if (searchQuery.value !== '' && select !== '') {
    if (select.value === '1') {
      getLikeName()
    } else if (select.value === '2') {
      getPriceScope()
    } else {
      ElMessage.error('请选择查询规则')
    }
  } else {
    ElMessage.error('请输入查询内容')
  }
}
//模糊查条件
const getLikeName = async () => {
  const response = await getOrderByProductName(
      currentPage.value, pageSize, searchQuery.value)
  orders.value = response.list;
  totalOrders.value = response.totalNum;
}
const getPriceScope = async () => {
  const priceScopeList = searchQuery.value.split('~')
  if (searchQuery.value.includes('~')) {
    if (priceScopeList[0] !== '' && priceScopeList[1] !== '') {
      const response = await getOrderByPriceScope(currentPage.value, pageSize,
          priceScopeList[0], priceScopeList[1])
      orders.value = response.list;
      totalOrders.value = response.totalNum;
    } else if (priceScopeList[0] === '') {
      const response = await getOrderByPriceScope(currentPage.value, pageSize,
          -1, priceScopeList[1])
      orders.value = response.list;
      totalOrders.value = response.totalNum;
    } else if (priceScopeList[1] === '') {
      const response = await getOrderByPriceScope(currentPage.value, pageSize,
          priceScopeList[0], -1)
      orders.value = response.list;
      totalOrders.value = response.totalNum;
    }
  } else {
    ElMessage.error('请按规则输入查询内容，是否缺失了~')
  }
}
const clearSearch = () => {
  if(searchQuery.value!==''){
  searchQuery.value = ''
  fetchOrders();}
}

//挂载函数
onMounted(() => {
  if (useTokenStore().token !== '') {
    fetchOrders();
  }else {
    router.push('/')
    ElMessage({
        showClose: true,
        message: '请先登录',
        type: 'error',
      })
  }
});

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchOrders();
};
// 获取订单列表
const fetchOrders = async () => {
  const response = await getOrderByTime(currentPage.value, pageSize);
  orders.value = response.list;
  totalOrders.value = response.totalNum;
};
const getDetail=(order)=>{
  const orderId =order.orderId
  router.push(`/orderDetail/${orderId}`)
}

</script>

<style scoped>
.el-button {
  margin-right: 10px;
}
</style>